//
//  ViewController.m
//  Compress
//
//  Created by 金色榜样研发组 on 2019/5/30.
//  Copyright © 2019 金色榜样研发组. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *password = @"123456";
//    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Video_1.mp4" ofType:@""];
//    NSString *cacheDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.rar",[[videoPath lastPathComponent] stringByDeletingPathExtension]]];
//    
//    URKArchive *rarArchive = [[URKArchive alloc] initWithPath:videoPath error:nil];
//    NSLog(@"rarArchive.compressedSize = %ld",[rarArchive.compressedSize integerValue]);
//    NSError *fileNamesError = nil;
//    NSArray *fileNames = [rarArchive listFilenames:&fileNamesError];
//    for (NSString *fileName in fileNames) {
//        NSLog(@"fileName = %@",fileName);
//    }
    
    
//    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Video_1.mp4" ofType:@""];
//    NSString *cacheDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",[[videoPath lastPathComponent] stringByDeletingPathExtension]]];
//    NSLog(@"cacheDirectoryPath = %@",cacheDirectoryPath);
    
//    单个文件压缩(无密码)
//    [self singleFileCompressNoPasswordWithZipFilePath:cacheDirectoryPath withContentsDirectory:videoPath];
//    单个文件压缩(有密码)
//    [self singleFileCompressWithZipFilePath:cacheDirectoryPath withContentsDirectory:videoPath WithPassword:password];
    
    
    NSString *directoryPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"妙偶天成"];
    NSString *zipFilePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",[directoryPath lastPathComponent]]];
    NSLog(@"zipFilePath = %@",zipFilePath);
    
//    多个文件压缩【文件夹中有多个文件】(无密码)
//    [self multipleFileCompressNoPasswordWithZipFilePath:zipFilePath withContentsDirectory:directoryPath];
//    多个文件压缩【文件夹中有多个文件】(有密码)
    [self multipleFileCompressWithZipFilePath:zipFilePath withContentsDirectory:directoryPath WithPassword:password];
    
//    多个文件压缩【文件数组，无法监听进度】(无密码)
//    [self multipleFileArrCompressNoPasswordWithZipFilePath:zipFilePath withContentsDirectory:directoryPath];
//    多个文件压缩【文件数组】(有密码)
//    [self multipleFileArrCompressWithZipFilePath:zipFilePath withContentsDirectory:directoryPath WithPassword:password];
    

    
}

#pragma mark ------------------------------------------ ZIP 压缩与解压缩 ------------------------------------------

#pragma mark -------------- 单个文件压缩(无密码) --------------
/** 单个文件压缩(无密码) */
-(void)singleFileCompressNoPasswordWithZipFilePath:(NSString *)zipFilePath withContentsDirectory:(NSString *)directory
{
//    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Video_1.mp4" ofType:@""];
//    NSString *cacheDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",[[videoPath lastPathComponent] stringByDeletingPathExtension]]];
//    NSLog(@"cacheDirectoryPath = %@",cacheDirectoryPath);
    
    if ([ZipRarManager createZipFileAtPath:zipFilePath withContentsDirectory:directory withPassword:@""]) {
        NSLog(@"压缩成功");
        [self singleFileDeCompressNoPassword:zipFilePath];
    }
    else
    {
        NSLog(@"压缩失败");
    }
}
#pragma mark -------------- 单个文件解压缩(无密码) --------------
/** 单个文件解压缩(无密码) */
-(void)singleFileDeCompressNoPassword:(NSString *)path
{
    NSString *cacheDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    if ([ZipRarManager unzipFileAtPath:path toDestination:cacheDirectoryPath withPassword:nil]) {
        NSLog(@"解压缩成功");
    }
    else
    {
        NSLog(@"解压缩失败");
    }
}


#pragma mark -------------- 单个文件压缩(有密码) --------------
/** 单个文件压缩(有密码) */
-(void)singleFileCompressWithZipFilePath:(NSString *)zipFilePath withContentsDirectory:(NSString *)directory WithPassword:(NSString *)password
{
//    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Video_1.mp4" ofType:@""];
//    NSString *cacheDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",[[videoPath lastPathComponent] stringByDeletingPathExtension]]];
//    NSLog(@"cacheDirectoryPath = %@",cacheDirectoryPath);
    
    if ([ZipRarManager createZipFileAtPath:zipFilePath withContentsDirectory:directory withPassword:password]) {
        NSLog(@"压缩成功");
        [self singleFileDeCompressPath:zipFilePath WithPassword:password];
    }
    else
    {
        NSLog(@"压缩失败");
    }
}
#pragma mark -------------- 单个文件解压缩(有密码) --------------
/** 单个文件解压缩(有密码) */
-(void)singleFileDeCompressPath:(NSString *)path WithPassword:(NSString *)password
{
    NSString *cacheDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    if ([ZipRarManager unzipFileAtPath:path toDestination:cacheDirectoryPath withPassword:password]) {
        NSLog(@"解压缩成功");
    }
    else
    {
        NSLog(@"解压缩失败");
    }
}


#pragma mark -------------- 多个文件压缩【文件夹中有多个文件】(无密码) --------------
/** 多个文件压缩【文件夹中有多个文件】(无密码) */
-(void)multipleFileCompressNoPasswordWithZipFilePath:(NSString *)zipFilePath withContentsDirectory:(NSString *)directory
{
    
    if ([ZipRarManager createZipFileAtPath:zipFilePath withContentsDirectory:directory withPassword:nil andProgressHandler:^(NSInteger currentIndex, NSInteger totalCount) {
        NSLog(@"currentIndex = %ld",currentIndex);
        NSLog(@"totalCount = %ld",totalCount);
    }]) {
        NSLog(@"压缩成功");
        [self multipleFileDeCompressNoPassword:zipFilePath];
    }
    else
    {
        NSLog(@"压缩失败");
    }
}
#pragma mark -------------- 文件解压缩【压缩包中是多个文件，可以监听进度】(无密码) --------------
/** 文件解压缩【压缩包中是多个文件，可以监听进度】(无密码) */
-(void)multipleFileDeCompressNoPassword:(NSString *)path
{
    NSString *cacheDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    
    
    if ([ZipRarManager unzipFileAtPath:path toDestinationn:cacheDirectoryPath withPassword:nil andProgressHandler:^(NSString * _Nonnull namePath, NSString * _Nonnull name, NSInteger currentIndex, NSInteger totalCount) {
        NSLog(@"namePath = %@",namePath);
        NSLog(@"name = %@",name);
        NSLog(@"currentIndex = %ld",currentIndex);
        NSLog(@"totalCount = %ld",totalCount);
    } completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nullable error) {
        NSLog(@"解压缩完成回调  解压缩完成回调  解压缩完成回调");
        NSLog(@"path = %@",path);
        if (succeeded) {
            NSLog(@"解压缩完成回调  解压缩完成回调  解压缩完成回调        成功");
        }
        else
        {
            NSLog(@"解压缩完成回调  解压缩完成回调  解压缩完成回调        失败：%@",error);
        }
    }]) {
        NSLog(@"解压缩成功");
    }
    else
    {
        NSLog(@"解压缩失败");
    }
}


#pragma mark -------------- 多个文件压缩【文件夹中有多个文件】(有密码) --------------
/** 多个文件压缩【文件夹中有多个文件】(有密码) */
-(void)multipleFileCompressWithZipFilePath:(NSString *)zipFilePath withContentsDirectory:(NSString *)directory WithPassword:(NSString *)password
{
//    NSString *directoryPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"妙偶天成"];
//    NSString *cacheDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",[directoryPath lastPathComponent]]];
//    NSLog(@"cacheDirectoryPath = %@",cacheDirectoryPath);
    if ([ZipRarManager createZipFileAtPath:zipFilePath withContentsDirectory:directory withPassword:password andProgressHandler:^(NSInteger currentIndex, NSInteger totalCount) {
        NSLog(@"currentIndex = %ld",currentIndex);
        NSLog(@"totalCount = %ld",totalCount);
    }]) {
        NSLog(@"压缩成功");
        [self multipleFileDeCompressWithPath:zipFilePath WithPassword:password];
    }
    else
    {
        NSLog(@"压缩失败");
    }
}
#pragma mark -------------- 文件解压缩【压缩包中是多个文件，可以监听进度】(有密码) --------------
/** 文件解压缩【压缩包中是多个文件，可以监听进度】(有密码) */
-(void)multipleFileDeCompressWithPath:(NSString *)path WithPassword:(NSString *)password
{
    NSString *cacheDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    
    
    if ([ZipRarManager unzipFileAtPath:path toDestinationn:cacheDirectoryPath withPassword:password andProgressHandler:^(NSString * _Nonnull namePath, NSString * _Nonnull name, NSInteger currentIndex, NSInteger totalCount) {
        NSLog(@"namePath = %@",namePath);
        NSLog(@"name = %@",name);
        NSLog(@"currentIndex = %ld",currentIndex);
        NSLog(@"totalCount = %ld",totalCount);
    } completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nullable error) {
        NSLog(@"解压缩完成回调  解压缩完成回调  解压缩完成回调");
        NSLog(@"path = %@",path);
        if (succeeded) {
            NSLog(@"解压缩完成回调  解压缩完成回调  解压缩完成回调        成功");
        }
        else
        {
            NSLog(@"解压缩完成回调  解压缩完成回调  解压缩完成回调        失败：%@",error);
        }
    }]) {
        NSLog(@"解压缩成功");
    }
    else
    {
        NSLog(@"解压缩失败");
    }
}


#pragma mark -------------- 多个文件压缩【文件数组，无法监听进度】(无密码) --------------
/** 多个文件压缩【文件数组，无法监听进度】(无密码) */
-(void)multipleFileArrCompressNoPasswordWithZipFilePath:(NSString *)zipFilePath withContentsDirectory:(NSString *)directory
{
    NSArray *directoryPathArr = [[NSBundle mainBundle] pathsForResourcesOfType:@"" inDirectory:@"妙偶天成"];
    NSString *cacheDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",[[[directoryPathArr lastObject] lastPathComponent] stringByDeletingPathExtension]]];
    NSLog(@"cacheDirectoryPath = %@",cacheDirectoryPath);
    
    if ([ZipRarManager createZipFileAtPath:cacheDirectoryPath withFilesAtPaths:directoryPathArr withPassword:@""]) {
        NSLog(@"压缩成功");
        [self multipleFileDeCompressNoPassword:cacheDirectoryPath];
    }
    else
    {
        NSLog(@"压缩失败");
    }
    
}



#pragma mark -------------- 多个文件压缩【文件数组】(有密码) --------------
/** 多个文件压缩【文件数组】(有密码) */
-(void)multipleFileArrCompressWithZipFilePath:(NSString *)zipFilePath withContentsDirectory:(NSString *)directory WithPassword:(NSString *)password
{
    NSArray *directoryPathArr = [[NSBundle mainBundle] pathsForResourcesOfType:@"" inDirectory:@"妙偶天成"];
    NSString *cacheDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",[[[directoryPathArr lastObject] lastPathComponent] stringByDeletingPathExtension]]];
    NSLog(@"cacheDirectoryPath = %@",cacheDirectoryPath);
    if ([ZipRarManager createZipFileAtPath:cacheDirectoryPath withFilesAtPaths:directoryPathArr withPassword:password]) {
        NSLog(@"压缩成功");
        [self multipleFileDeCompressWithPath:cacheDirectoryPath WithPassword:password];
    }
    else
    {
        NSLog(@"压缩失败");
    }
}




@end
